﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;

namespace THO7AlgoritmTimerApplication
{
    public unsafe class Image
    {
        private Bitmap source;
        private BitmapData data;
        private byte* pnt;

        public Image(Bitmap source)
        {
            this.source = source;
        }

        public Image(int height, int width)
        {
            source = new Bitmap(width, height);
        }

        public void Lock()
        {
            data = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            pnt = (byte*)data.Scan0;
        }

        public Bitmap Unlock()
        {
            source.UnlockBits(data);
            return source;
        }

        public void SetPixel(int x, int y, PixelColor color)
        {
            int position = (y * data.Stride) + (x * 3);
            pnt[position] = color.R;
            pnt[position + 1] = color.G;
            pnt[position + 2] = color.B;
        }

        public PixelColor GetPixel(int x, int y)
        {
            int position = (y * data.Stride) + (x * 3);
            int blue = pnt[position];
            int green = pnt[position + 1];
            int red = pnt[position + 2];

            return  new PixelColor(red, green, blue);
        }
        public int GetPixelG(int x, int y)
        {
            return pnt[(y * data.Stride) + (x * 3) + 1];
        }

        public int GetPixelR(int x, int y)
        {
            return pnt[(y * data.Stride) + (x * 3) + 2];
        }

        public int GetPixelB(int x, int y)
        {
            return pnt[(y * data.Stride) + (x * 3)];
        }

        public double[] getHistogramFromFilter(int x, int y, int filterWidth)
        {
            double[] histogram = new double[30];
            double[] histogramR = new double[10];
            double[] histogramG = new double[10];
            double[] histogramB = new double[10];

            int filtersize = filterWidth * filterWidth;

            for (int fX = x - filterWidth / 2; x <= (filterWidth / 2) + y; fX++)
            {
                for (int fY = y - filterWidth / 2; y <= (filterWidth / 2) + y; fY++)
                {
                    if (x < 0 || x >= data.Stride || y < 0 || y >= data.Height)
                    {
                        filtersize--;
                    }
                    else
                    {
                        int position = pnt[(y * data.Stride) + (x * 3)];
                        PixelColor c = GetPixel(x, y);
                        histogramR[(int)(c.R * 9)]++;
                        histogramG[(int)(c.G * 9)]++;
                        histogramB[(int)(c.B * 9)]++;
                    }
                }
            }

            int ix = 0;
            for (int i = 0; i < 10; i++)
            {
                histogram[ix] = histogramR[i];
                histogram[ix + 10] = histogramG[i];
                histogram[ix + 20] = histogramB[i];
                ix++;
            }

            ///check for middle value here
            for (int i = 0; i < 30; i++)
            {
                histogram[i] /= (filtersize);
            }

            return histogram;
        }
    }
}
